import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
import { getParentMenuList } from '@/service/module/main/system/menu-list'

interface IMenuListState {
  parentMenuList: any[]
}

const initialState: IMenuListState = {
  // 上级菜单：新增或者编辑要获取下拉框形式的菜单树
  parentMenuList: []
}

// 获取上级菜单数据
export const fetchParentMenuListAction = createAsyncThunk(
  'fetch/parent-menu-list',
  async (_, { dispatch }) => {
    const res = await getParentMenuList()
    dispatch(changeParentMenuListAction(res.data))
    return res.data
  }
)

const menuListSlice = createSlice({
  name: 'menu-list',
  initialState,
  reducers: {
    changeParentMenuListAction(state, { payload }) {
      state.parentMenuList = payload
    }
  }
})

export const { changeParentMenuListAction } = menuListSlice.actions
export default menuListSlice.reducer
